(ns acst.models.comments
  (:use acst.config.error-config
        acst.config.success-config
        acst.utils.handle-exp)
  (:require [acst.models.db :as db]
            [acst.models.roles :refer [if-is-admin]]
            [acst.utils.escape :refer [html-sanitizer]]
            [clojure.string :refer [split]]))


(defn- get-key-name [table_name]
  (str (first (split (name table_name) #"_")) "_id"))

(defn- get-reply-root-id [table_name key_name reply_id key_id]
  (first
    (db/query-1
      [(str "SELECT comment_id, reply_root_id FROM " table_name
            " WHERE comment_id = ? AND " key_name "=? ") reply_id key_id]))) ;;确保要回复的评论在当前话题下

(defn- update-reply-root-id! [table_name comment_id]
  (db/update! table_name
              {:reply_root_id comment_id}
              ["comment_id = ?" comment_id]))

(defn- get-and-update-reply-root-id! [table_name key_id reply_id]
  (if (nil? reply_id)
    nil
    (let [real_table_name (name table_name)
          key_name (name (get-key-name table_name))
          result (get-reply-root-id real_table_name key_name, reply_id key_id)]
      ;;假如comment_id是root_id, 那么reply_root_id 不存在, update它
      (if (and (nil? (:reply_root_id result)) (:comment_id result))
        (update-reply-root-id! table_name (:comment_id result)))
      ;;如何root_id不存在那么reply_id(comment_id)就是根,comment_id不存在即reply_id被篡改
      (or (:reply_root_id result) (:comment_id result) (:comment-failed comments-error)))))

(defn insert-comment! [table_name key_id publisher_id content reply_id]
  (let [key_name (keyword (get-key-name table_name))
        reply_root_id (get-and-update-reply-root-id! table_name key_id reply_id)] ;;从talbename拼接keyname
    (println "reply_root_id" reply_root_id)
    (if
      (and (seq? reply_root_id) (get-error reply_root_id))
      reply_root_id                                         ;;此时reply_root_id 是错误信息

      (let [result
            (db/insert!
              table_name
              {key_name key_id
               :publisher_id publisher_id
               :content (html-sanitizer content)
               :reply_root_id reply_root_id
               :reply_id reply_id
               :status 1})]                        ;;如果reply_id不合法,在get-reply-root-id被晒掉
        (if (get-error result)
          (:comment-failed comments-error)
          result))))) ;;从table_name中获取外键名


(defn get-all-comment [table_name key_id limit page]
  (let [key_name (get-key-name table_name)]
    (db/query (:comment-not-found comments-error)
              [(str "SELECT username, comment_id, create_date, publisher_id, content, reply_root_id, reply_id
                     FROM users, " (name table_name) " WHERE " key_name "=" key_id "AND user_id = publisher_id AND status = 1
                     ORDER BY create_date DESC LIMIT ? OFFSET ?")
               limit (* (- page 1) limit)])))

(defn get-comment-reply-tree [table_name key_id comment_id]
  (let [_table_name (name table_name)
        key_name (get-key-name table_name)
        {reply_root_id :reply_root_id}
        ;;通过树的任意节点获取根id
        (first (db/query-1 [(str "SELECT comment_id, create_date, publisher_id, username, content, reply_root_id, reply_id
                            FROM users, " _table_name " WHERE user_id = publisher_id AND comment_id= ? AND "
                                 key_name "= ?") comment_id key_id]))]
    (if (nil? reply_root_id)
      ;;假如root_id为空,此评论无回复
      (:reply_not_found comments-error)

      (db/query (:reply_not_found comments-error)
                [(str "SELECT comment_id, create_date, publisher_id, username, content, reply_root_id, reply_id
                       FROM users, " _table_name " WHERE user_id=publisher_id AND reply_root_id=? AND " key_name "= ?") comment_id key_id]))))
(defn delete-comment! [table_name key_id comment_id user_id]
  (let [key_name (get-key-name table_name)]
    (if
      (or (= '(1) (db/update! table_name
                              {:status 0
                               :content "该评论已删除"}
                              [(str key_name  " = ? AND comment_id = ? AND publisher_id = ?") key_id comment_id user_id]))
          (= '(1) (if-is-admin user_id (db/update! table_name
                                                  {:status 0
                                                   :content "该评论已删除"}
                                                 ["comment_id = ?" comment_id]))))
      (:delete-memo-success memos-result)
      (:delete-memo-failed memos-error))))